package com.shouhuobao.bhi.loc;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Message;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.collectplus.express.logic.AppCache;
import com.collectplus.express.logic.AppConfig;
import com.collectplus.express.logic.MsgID;

import droid.frame.activity.HandlerMgr;
import droid.frame.utils.lang.Str;

/**
 * 定位服务<br>
 *
 * @author coffee<br>
 * 2015-4-24上午10:29:17
 */
public class BaiduLocationService extends Service implements BDLocationListener {

    // 定位相关
    private LocationClient mLocClient;
    //
    private int msgId = -1;

    @Override
    public void onCreate() {
        super.onCreate();
        // 定位初始化
        mLocClient = new LocationClient(this);
        mLocClient.registerLocationListener(this);
        LocationClientOption option = new LocationClientOption();
        option.setIsNeedAddress(true);// 设置以后,请求结果 BDLocation#getCity 就不为null了
        option.setOpenGps(true);// 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(AppConfig.getBadiuLocBeat());// 定位频率
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        option.setIgnoreKillProcess(false);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤GPS仿真结果，默认需要

        mLocClient.setLocOption(option);
        mLocClient.start();
    }

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            msgId = intent.getIntExtra("msgId", -1);
        }
        // 刷新定位
        mLocClient.requestLocation();
        //
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 定位SDK监听函数
     */

    @Override
    public void onReceiveLocation(final BDLocation location) {
        try {
            Message msg = Message.obtain();
            if (msgId == -1) {
                msg.what = MsgID.location_baidu;
            } else {
                msg.what = msgId;
                msgId = -1;// reset
            }
            if (location != null && Str.isNotEmpty(location.getCity())) {
                if (AppConfig.isOtherCity != 0) {
                    location.setLatitude(AppConfig.getOhterCityLat());
                    location.setLongitude(AppConfig.getOhterCityLng());
                    AppCache.setCurrentCityName(AppConfig.getCityName());
                } else {
                    AppCache.setCurrentCityName(location.getCity());
                }
            }
            msg.obj = location;
            HandlerMgr.sendMessage(msg, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 退出时销毁定位
        mLocClient.stop();
    }

}
